package com.kd.controller;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.kd.common.C_UserLoginToken;
import com.kd.common.E_UserLoginToken;
import com.kd.common.PageResult;
import com.kd.common.Result;
import com.kd.dao.ProductMapper;
import com.kd.entity.Employee;
import com.kd.entity.Product;
import com.kd.service.product.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("product")
public class ProductController {



    @Autowired
    ProductService productService;



    @RequestMapping("/list/filter")
    @ResponseBody
    @E_UserLoginToken
    public Result<PageResult<Product,Integer>> getProductsWithFilters(
            @RequestParam(required = false) Integer cursor,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Integer pid,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer sidefork,
            @RequestParam(required = false) Integer panel_leg,
            @RequestParam(required = false) Integer customize,
            @RequestParam(required = false) Integer t_bottomlayer,
            @RequestParam(required = false) Integer t_leglayout,
            @RequestParam(required = false) Integer tf_boardwing,
            @RequestParam(required = false) String f_structure,
            @RequestParam(required = false) Integer f_blockheight,
            @RequestParam(required = false) BigDecimal minPrice,
            @RequestParam(required = false) BigDecimal maxPrice

    ) {

        try {
            PageResult<Product,Integer> pageResult = productService.findProductsWithFilters(
                    cursor, limit, pid, name, sidefork, panel_leg,customize,
                    t_bottomlayer, t_leglayout, tf_boardwing, f_structure,
                    f_blockheight, minPrice, maxPrice);
            return Result.success(pageResult,  "查询成功");
        } catch (Exception e) {
            return Result.fail(500,  "查询失败: " + e.getMessage());
        }
    }

    @RequestMapping("/list/cfilter")
    @ResponseBody
    @C_UserLoginToken
    public Result<PageResult<Product,Integer>> getProductsWithcFilters(
            @RequestParam(required = false) Integer cursor,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(required = false) Integer pid)
    {
        try {
            PageResult<Product,Integer> pageResult = productService.findProductsWithFilters(
                    cursor, limit, pid);
            return Result.success(pageResult,  "查询成功");
        } catch (Exception e) {
            return Result.fail(500,  "查询失败: " + e.getMessage());
        }
    }


    @RequestMapping("/add")
    @ResponseBody
    public Result<Product> addProduct(@RequestBody Product product) {
        try {
            Product result = productService.matchProduct(product);

            //若存在
            if (result != null) {
                Product product1 = new Product();
                product1.setPrice(result.getPrice());
                product1.setPid(result.getPid());
                return Result.success(product1, "产品已存在");
            } else {

                //若不存在
                boolean success = productService.insertProduct(product);
                if (success) {
                    Product result1 = productService.matchProduct(product);
                    Product product1 = new Product();
                    product1.setPrice(result1.getPrice());
                    product1.setPid(result1.getPid());
                    return Result.success(product1, "添加成功");
                }
                return Result.fail(500, "产品添加失败");
            }
        } catch(Exception e){
                return Result.fail(500, "产品添加失败: " + e.getMessage());
            }

    }


//@RequestMapping("/del")
//@ResponseBody
//public Result<Void> deleteProduct(@PathVariable Integer pid) {
//    try {
//        boolean success = productService.deleteProductById(pid);
//        if (success) {
//            return Result.success(null,  "产品删除成功");
//        }
//        return Result.fail(404,  "未找到该产品");
//    } catch (Exception e) {
//        return Result.fail(500,  "产品删除失败: " + e.getMessage());
//    }
//}

    @RequestMapping("/update")
    @ResponseBody
    public Result<Product> updateProduct(@RequestBody Product product) {
        try {
            productService.updateProduct(product);
            return Result.success( product, "更新成功");
        } catch (Exception e) {
            return Result.fail(500,  "更新失败: " + e.getMessage());
        }
    }

    @GetMapping("/stats")
    @E_UserLoginToken
    public Result<PageResult<ProductMapper.ProductStats, Integer>> getProductStats(
            @RequestParam(required = false) Integer cursor,  // 改为Integer类型匹配psq字段
            @RequestParam(defaultValue = "10") Integer limit,  // 添加分页大小限制
            @RequestParam(defaultValue = "next") String direction) {
        // 参数校验
        if (limit <= 0 || limit > 100) {
            throw new IllegalArgumentException("分页大小限制为1-100");
        }
        // 执行查询
        PageResult<ProductMapper.ProductStats, Integer> result = productService.getProductStats(
                cursor,
                limit,
                "next".equalsIgnoreCase(direction)
        );
        return Result.success(result,  "查询成功");
    }


}